package com.pe1n.yangtaopai.page.find.view.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.adapter.MyFindGoodsAdapter;
import com.pe1n.yangtaopai.bean.FindGoodsBean;
import com.pe1n.yangtaopai.page.find.presenter.PresenterImpl;
import com.pe1n.yangtaopai.page.find.view.interfacese.IView;
import com.pe1n.yangtaopai.utils.URLConstant;

import java.util.ArrayList;
import java.util.List;

public class FindActivity extends AppCompatActivity implements IView{

    private Context mContext = this;
    private ProgressBar progressBar;
    private RecyclerView recyclerView;
    private ImageView imageViewBack,imageViewImg;
    private TextView textViewTitle,textViewTitleGoods,textViewMsg;
    private PresenterImpl mPresenter;
    private String id,title;
    private FindGoodsBean mFindGoodsBean;
    private List<FindGoodsBean.DataBean.ProdsInfoBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);

        mPresenter = new PresenterImpl(this);
        initData();
        initView();

    }

    private void initData() {

        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        title = intent.getStringExtra("title");
        //Log.e("TAG","-------------------------   GET   " + id);
        String url = String.format(URLConstant.FIND_GOOS_URL,id);
        mPresenter.loadFindGoodsBeanData(url);

    }

    private void initView() {

        progressBar = (ProgressBar) findViewById(R.id.progress_bar_find_goods);
        textViewTitle = (TextView) findViewById(R.id.tv_title_find);
        textViewTitle.setText(title);
        textViewTitleGoods = (TextView) findViewById(R.id.tv_title_text_find);
        textViewMsg = (TextView) findViewById(R.id.tv_msg_find);
        imageViewBack = (ImageView) findViewById(R.id.iv_bac_find);
        imageViewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        imageViewImg = (ImageView) findViewById(R.id.iv_img_find);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_goods_find);

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showLoadFailMsg() {

    }

    @Override
    public void loadFindGoodsBeanData(FindGoodsBean findGoodsBean) {
        mFindGoodsBean = findGoodsBean;
        mList = findGoodsBean.getData().getProdsInfo();
        Log.e("TAG","--------------FIND--------------  " + mList.size());
        loadData();
    }

    private void loadData() {
        textViewTitleGoods.setText(mFindGoodsBean.getData().getTitle());
        textViewMsg.setText(mFindGoodsBean.getData().getIntro());
        Glide.with(mContext).load(mFindGoodsBean.getData().getImagePath()).placeholder(R.drawable.place_holder).into(imageViewImg);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, OrientationHelper.VERTICAL,false));
        recyclerView.setAdapter(new MyFindGoodsAdapter(mContext,mList));
    }
}
